<template>
    <div>
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item>
                    <i class="el-icon-lx-cascades"></i> 停车点
                </el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <div class="container">
            <div class="handle-box">
                <el-button
                        type="primary"
                        icon="el-icon-plus"
                        class="handle-del mr10"
                        @click="handleAdd"
                >添加车位</el-button>
                <el-select v-model="query.manageArea" placeholder="区域" class="handle-select mr10">
                    <el-option key="0" label="全部" value=""></el-option>
                    <el-option v-for="(road,i) in roadData" :key="i" :label="road.name" :value="road.roadName"></el-option>
                </el-select>
                <el-button type="primary" icon="el-icon-search" @click="handleSearch">搜索</el-button>
            </div>
            <el-table
                    :data="tableData"
                    border
                    class="table"
                    ref="multipleTable"
                    header-cell-class-name="table-header"
                    @selection-change="handleSelectionChange"
            >
                <el-table-column prop="id" label="ID" width="55" align="center"></el-table-column>
                <el-table-column prop="manageArea" label="区域"></el-table-column>
                <el-table-column prop="manageStaId" label="车位号"></el-table-column>
                <el-table-column prop="managePs" label="备注"></el-table-column>
                <el-table-column prop="manageCarId" label="当前车辆"></el-table-column>

                <el-table-column label="操作" width="180" align="center">
                    <template slot-scope="scope">
                        <el-button
                                type="text"
                                icon="el-icon-edit"
                                @click="handleEdit(scope.$index, scope.row)"
                        >编辑</el-button>
                        <el-button v-if="scope.row.manageDelete == 0"
                                type="text"
                                icon="el-icon-remove-outline"
                                class="red"
                                @click="handleDelete(scope.$index, scope.row)"
                        >点击禁用</el-button>
                        <el-button v-if="scope.row.manageDelete == 1"
                                type="text"
                                icon="el-icon-circle-check"
                                class="green"
                                @click="handleStart(scope.$index, scope.row)"
                        >点击启用</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div class="pagination">
                <el-pagination
                        background
                        layout="total, prev, pager, next"
                        :current-page="query.pageIndex"
                        :page-size="query.pageSize"
                        :total="pageTotal"
                        @current-change="handlePageChange"
                ></el-pagination>
            </div>
        </div>

        <!-- 添加车位弹出框 -->
        <el-dialog title="添加车位" :visible.sync="addVisible" width="30%">
            <el-form ref="form" :model="addForm" label-width="70px">
                <el-form-item label="区域">
                    <el-select v-model="addForm.manageArea" placeholder="区域" class="handle-select mr10">
                        <el-option v-for="(road,i) in roadData" :key="i" :label="road.name" :value="road.roadName"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="车位号">
                    <el-input v-model="addForm.manageStaId"></el-input>
                </el-form-item>
                <el-form-item label="备注">
                    <el-input type="textarea" :rows="2" v-model="addForm.managePs"></el-input>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="addVisible = false">取 消</el-button>
                <el-button type="primary" @click="saveAdd">确 定</el-button>
            </span>
        </el-dialog>

        <!-- 编辑弹出框 -->
        <el-dialog title="编辑" :visible.sync="editVisible" width="30%">
            <el-form ref="form" :model="form" label-width="70px">
                <el-form-item label="ID">
                    <el-input disabled="true" v-model="form.id"></el-input>
                </el-form-item>
                <el-form-item label="区域">
                    <el-select disabled="true" v-model="form.manageArea" placeholder="区域" class="handle-select mr10">
                        <el-option v-for="(road,i) in roadData" :key="i" :label="road.name" :value="road.roadName"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="车位号">
                    <el-input disabled="true" v-model="form.manageStaId"></el-input>
                </el-form-item>
                <el-form-item label="备注">
                    <el-input type="textarea" :rows="2" v-model="form.managePs"></el-input>
                </el-form-item>
                <el-form-item label="当前车辆">
                    <el-input disabled="true" v-model="form.manageCarId"></el-input>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="editVisible = false">取 消</el-button>
                <el-button type="primary" @click="saveEdit">确 定</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>
    import { getRoads,addParking, disableParking, enableParking, getParkingList, updateParking } from '../../api/index';
    export default {
        name: 'parkingInfo',
        data() {
            return {
                query: {
                    manageArea: '',
                    pageIndex: 1,
                    pageSize: 10
                },
                tableData: [],
                roadData:[],
                multipleSelection: [],
                delList: [],
                editVisible: false,
                addVisible: false,
                pageTotal: 0,
                form: {},
                addForm:{},
                idx: -1,
                id: -1
            };
        },
        created() {
            this.getData();
        },
        methods: {
            // 获取 easy-mock 的模拟数据
            async getData() {
                const result = await getParkingList(this.query);
                // 查询所有路段
                const resultRoad = await getRoads();
                console.log(resultRoad);
                console.log(result);
                this.roadData = resultRoad.data;
                this.tableData = result.rows;
                this.pageTotal = result.total || 50;
            },
            // 触发搜索按钮
            handleSearch() {
                this.$set(this.query, 'pageIndex', 1);
                this.getData();
            },
            // 禁用操作
            async handleDelete(index, row) {
                const result = await disableParking({id:row.id});
                if (result.status == 200) {
                    this.$message.success('禁用成功');
                    this.getData();
                } else {
                    this.$message.error('禁用失败');
                }
            },
            // 启用操作
            async handleStart(index, row) {
                const result = await enableParking({id:row.id});
                if (result.status == 200) {
                    this.$message.success('启用成功');
                    this.getData();
                } else {
                    this.$message.error('启用失败');
                }
            },
            // 多选操作
            handleSelectionChange(val) {
                this.multipleSelection = val;
            },
            // 添加操作
            handleAdd() {
                this.addVisible = true;
            },
            // 添加提交
            async saveAdd(){
                this.addVisible = false;

                const result = await addParking(this.addForm);
                console.log(result);
                if (result.status == 200) {
                    this.$message.success('添加成功');
                    this.getData();
                } else {
                    this.$message.error('添加失败');
                }

            },

            // 编辑操作
            handleEdit(index, row) {
                this.idx = index;
                this.form = row;
                this.editVisible = true;
            },
            // 保存编辑
            async saveEdit() {
                this.editVisible = false;


                const result = await updateParking(this.form);
                console.log(result);
                if (result.status == 200) {
                    this.$message.success('编辑成功');
                    this.getData();
                } else {
                    this.$message.error('编辑失败');
                }
            },
            // 分页导航
            handlePageChange(val) {
                this.$set(this.query, 'pageIndex', val);
                this.getData();
            }
        }
    };
</script>

<style scoped>
    .handle-box {
        margin-bottom: 20px;
    }

    .handle-select {
        width: 120px;
    }

    .handle-input {
        width: 300px;
        display: inline-block;
    }
    .table {
        width: 100%;
        font-size: 14px;
    }
    .red {
        color: #ff0000;
    }
    .mr10 {
        margin-right: 10px;
    }
    .table-td-thumb {
        display: block;
        margin: auto;
        width: 40px;
        height: 40px;
    }
</style>
